 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace RestorationIce
{
	partial class RestoIce
	{
		#region Action

		private bool Interrupt()
		{
			if (Me.IsCasting && (lastCast != null && !lastCast.Dead && lastCast.HealthPercent >= PVE_do_not_heal_above) && isinterrumpable)
			{
				SpellManager.StopCasting();
				if (lastCast != null)
				{
					slog(Color.Brown, "Interrupting Healing, target at {0} %", Round(lastCast.HealthPercent));
				}
				lastCast = null;
				isinterrumpable = false;
				return true;
			} 
			else if (Me.IsCasting)
			{

				return true;
			}
			else
			{
				return false;
			}
		}

		public bool NeedtoRest()
		{
			if (Me.ManaPercent > 20 && usedBehaviour != "Battleground" && Resurrecting()) { return true; }

			else if ((!Me.Combat) && (Me.ManaPercent <= rest_if_mana_below) && (!Me.HasAura("Drink")) && (!Me.IsMoving) && (!Me.Mounted))
			{
				slog(Color.Blue, "#Out of Combat - Mana is at {0} %. Time to drink.#", Round(Me.ManaPercent));
				Styx.Logic.Common.Rest.Feed();
				return true;
			}

			// This is to ensure we STAY SEATED while eating/drinking. No reason for us to get up before we have to.
			if ((!Me.Combat) && (Me.HasAura("Drink") && (Me.ManaPercent < 95)))
			{
				return true;
			}

			return false;
		}


		//private bool EnemyInterrupt(bool rebuke, bool HoJ, bool also_move)
		//{
		//    bool casted = false, thiscasted = false;
		//    if (Enemy != null && Enemy.IsValid && !Enemy.Dead && Enemy.IsCasting)
		//    {
		//        if (rebuke)
		//        {
		//            //slog(Color.DarkMagenta, "Casting Rebuke on {0} at {1}", Enemy.Name, Round(Enemy.Distance));
		//            thiscasted = Cast("Rebuke", Enemy, 5, "EnemyInterrupt", "Interrupting");
		//            if (thiscasted) { casted = true; }
		//        }
		//        if (HoJ && Enemy.HealthPercent < 50)
		//        {
		//            slog(Color.DarkMagenta, "Wanna Hammer of Justice on {0} at {1}", Enemy.Name, Round(Enemy.Distance));
		//            if (also_move && Enemy.Distance > 10 && Enemy.Distance < 40)
		//            {
		//                slog(Color.DarkMagenta, "Moving in to Hammer of Justice");
		//                MoveTo(Enemy);
		//            }
		//            else
		//            {
		//                //slog(Color.DarkMagenta, "Casting Hammer of Justice on {0} at {1}", Enemy.Name, Round(Enemy.Distance));
		//                thiscasted = Cast("Hammer of Justice", Enemy, 10, "EnemyInterupt", "Interrupting"); ;
		//                if (thiscasted) { casted = true; }
		//            }
		//        }
		//    }
		//    return casted;
		//}


		private readonly List<uint> _potionEid = new List<uint>
		{
				//=== RESTORATION POTIONS (HEALTH AND MANA)
		57193,  // Mighty Rejuvenation Potion 8000
		67415,  // Draught of War 8000
		40077,  // Crazy Alchemist's Potion 4400 (Alchemist)
		34440,  // Mad Alchemist's Potion 2750   (Alchemist)
		40087,  // Powerful Rejuvenation Potion 4125
		22850,  // Super Rejuvenation Potion 2300
		18253,  // Major Rejuvenation Potion 1760
		9144,   // Wildvine Potion 1500
		2456,   // Minor Rejuvenation Potion 150

			//=== MANA POTIONS 
		57192, // 10000 Mythical Mana Potion
		43570, // 3000 Endless Mana Potion 
		33448, // 4400 Runic Mana Potion
		40067, // 3000 Icy Mana Potion
		31677, // 3200 Fel Mana Potion
		33093, // 3000 Mana Potion Injector
		43530, // 3000 Argent Mana Potion
		32948, // 3000 Auchenai Mana Potion
		22832, // 3000 Super Mana Potion
		28101, // 2250 Unstable Mana Potion
		13444, // 2250 Major Mana Potion
		13443, // 1500 Superior Mana Potion
		6149, // 900 Greater Mana Potion
		3827, // 585 Mana Potion
		3385, // 360 Lesser Mana Potion
		2455, // 180 Minor Mana Potion
		};

		private bool Self()
		{
			if (!manapot.IsRunning) { manapot.Start(); }
			bool casted = false;
			if (sw.Elapsed.TotalSeconds > use_mana_rec_trinket_every && Me.Combat && Me.ManaPercent <= use_mana_rec_trinket_on_mana_below)
			{
				Lua.DoString("UseItemByName(\"" + "Tyrande\'s Favorite Doll" + "\")");
				slog(Color.Blue, "Mana at {0} % using Tyrande\'s Favorite Doll", Round(Me.ManaPercent));
				sw.Reset();
				sw.Start();
			}
			if (use_mana_potion && Me.ManaPercent <= use_mana_potion_below && Me.Combat && manapot.ElapsedMilliseconds >= 60000)
			{
				WoWItem potion = CheckForItem(_potionEid);
				if (potion != null)
				{ 
					slog(Color.Crimson,"#POTION: *" + potion.Name + "*");
					Lua.DoString("UseItemByName(\"" + potion.Name + "\")");
					StyxWoW.SleepForLagDuration();
					manapot.Reset();
					manapot.Start();                    
				}                
			}  
			return casted;
		}

		private bool rebirth()
		{
			if (userebirth & tank.Dead && Me.Combat && ShouldRebirth())
			{				
				return Cast("Rebirth", tank, 40, "Rezing the Tank he must have f'ed up sure wasn't me I'm a computer!");
			}
			return false;
		}

		private static WoWItem CheckForItem(List<uint> listId)
		{
			List<WoWItem> itemList = ObjectManager.GetObjectsOfType<WoWItem>(false);

			return itemList.FirstOrDefault(item => listId.Contains(item.Entry));
		}

		private bool ManaRec(int Inntervate_min)
		{
			if (Me.ManaPercent <= Inntervate_min && CanCast("Innervate") && Me.Combat)
			{
				Cast("Innervate",Me ,"Mana is low, Casting Inntervate");
				slog(Color.Blue, "Mana is below {0} % using Innervate", Inntervate_min);
				return true;
			}
			return false;
		}

		private bool RemovingCorruption()
		{
			WoWPlayer p = GetRemoveCorruptionTarget();
			//slog(Color.Violet, "RemovingCorruption {0}", p.Name);
			return Cast("Remove Corruption", p, 40, "Remove Corruption", "Noone to heal, dispelling");
		}
		private bool Dps(bool DPS)
		{
			if (DPS && CanCast("Wrath") && Me.Combat && Enemy != null && Enemy.IsValid && !Enemy.Dead)
			{
				if (wanna_face) { WoWMovement.Face(); }
				//slog(Color.DarkMagenta, "Casting Hammer of Wrath on {0} at {1}", Enemy.Name, Round(Enemy.Distance));
				return Cast("Wrath", Enemy, 30, "DPS", "nothing better to do...");
			}
			return false;
		}

		private bool TopOff()
		{
			if (tar != null)
			{
				if (tank != null && (Me.Mounted && !Me.Combat && !tank.Combat) || ((!Me.Combat && !tar.Combat && tar.Distance > 40)))
				{
					return false;
				}
				else if (tank == null)
				{
					return false;
				}
				if (!(Me.IsMoving) && (Me.Combat))
				{
					//WoWMovement.Face();
					isinterrumpable = true; return Cast("Healing Touch", tar, 40, "Heal", "Topping people off");
					//slog(Color.Green, "Topping off {0} at {1} %", tar.Name, Round(tar.HealthPercent));
				}
				else if (!(Me.IsMoving) && !(Me.Combat))
				{
				//WoWMovement.Face();
					isinterrumpable = true; return Cast("Nourish", tar, 40, "Heal", "Topping people off");
					//slog(Color.Green, "Topping off {0} at {1} %", tar.Name, Round(tar.HealthPercent));
				}
			}
			return false;
		}

		private bool ConsumeTime(bool Wrath)
		{           
			if (Wrath && Me.Combat && Enemy != null && Enemy.IsValid && !Enemy.Dead)
			{
				if (wanna_face) { WoWMovement.Face(); }
				//slog(Color.DarkMagenta, "Casting Crusader Strike on {0} at {1}", Enemy.Name, Round(Enemy.Distance));
				return Cast("Wrath", Enemy, 40, "DPS", "Melee range, free HP");
			}
			return false;
		}

		private bool Buff(bool should_move)
		{
			foreach (WoWPlayer p in Me.PartyMembers)
			{
				if (p.Distance < 0 || p.Distance > 40 || p.Dead || p.IsGhost || !p.InLineOfSight)
					continue;
				if ((!GotBuff("Mark of the Wild", p) && (!Me.Mounted) && p.Distance < 40 && p.InLineOfSight))
				{                    
					return Cast("Mark of the Wild", p, 40, "Casting Mark of the Wild on {0}", p.Name);
				}
			}

			return false;
		}

		private bool MountUp()
		{
		  //  if (noncombatfrom.Elapsed.TotalSeconds > 2)
		  //  {
		  //  WoWPlayer localtank;
		  //  localtank = GetTank();
		  //  if (localtank==null){return false;}else
			if (!tank.IsValid) { return false; }
			if (!Me.IsValid) { return false; }
				if (tank != null && Me != null && !tank.Dead && !Me.Dead && tank.Mounted && !Me.Mounted && !Me.Combat && !tank.Combat && Me != tank && Mount.CanMount())
				{
					WoWMovement.MoveStop();
					Mount.MountUp();
					slog(Color.DarkOrange, "Mounting UP to follow the tank!");
					lastCast = null;
					return true;
				}
				else if (tank != null && Me != null && Me.Mounted && !Me.Combat && !tank.Combat)
				{
					slog(Color.DarkOrange, "We are mounted... chill");
					lastCast = null;
					return true;
				}
				noncombatfrom.Reset();
				noncombatfrom.Start();
				return false;
		//    }
		//    return false;

		}


	}

		#endregion
 }